package com.kakavr.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 验证
 * 
 * @author gang.yang
 */
public class ValidatorUtils {
	/**
	 * 正则表达式：验证手机号
	 */
	private static final String REGEX_MOBILE = "^(1)\\d{10}$";
	/**
	 * 正则表达式：验证固定电话
	 */
	private static final String REGEX_PHONE = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|"
			+ "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";
	/**
	 * 验证用户名
	 */
	private static final String USERNAME = "^[a-zA-Z0-9]{6,20}$";
	/**
	 * 验证邮编
	 */
	private static final String POSTCODE = "[1-9]\\d{5}(?!\\d)";
	/**
	 * 验证邮编
	 */
	private static final String EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

	/**
	 * 验证金额
	 */
	private static final String MONEY = "^((-?[1-9]{1}\\d{0,9})|(-?[0]{1}))(\\.(\\d){0,4})?$";
	/**
	 * 验证正数金额
	 */
	private static final String PLUSMONEY = "^(([1-9]{1}\\d{0,9})|([0]{1}))(\\.(\\d){0,4})?$";
	/**
	 * 验证只有汉字和字母
	 */
	private static final String SINOGRAMORLETTER = "^[a-zA-Z\u4E00-\u9FA5]+$";

	/**
	 * 只有数字
	 */
	private static final String IS_NUMBER = "^[0-9]+?$";

	/**
	 * 验证邮编
	 * @param postCode
	 * @return
	 */
	public static boolean isPostCode(String postCode) {
		if (StringUtils.isNotBlank(postCode)) {
			postCode.matches(POSTCODE);
		}
		return false;
	}
	/**
	 * 验证邮编
	 * @param postCode
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (StringUtils.isNotBlank(email)) {
			email.matches(EMAIL);
		}
		return false;
	}

	/**
	 * 判断是否是数字
	 * 
	 * @param num
	 * @return true:是数字 false:不是数字
	 */
	public static boolean isNumber(String num) {
		return null != num && num.matches(IS_NUMBER);
	}

	/**
	 * 校验手机号
	 * 
	 * @param mobile
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isMobile(String mobile) {
		return null != mobile && mobile.matches(REGEX_MOBILE);
	}

	/**
	 * 校验固定电话
	 * 
	 * @param mobile
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isPhone(String mobile) {
		return null != mobile && mobile.matches(REGEX_PHONE);
	}

	/**
	 * 校验密码
	 * 
	 * @param password
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isPass(String password) {
		if (null == password) {
			return false;
		}
		int passLen = password.trim().length();
		if (passLen > 5 && passLen < 17) {
			return true;
		}
		return false;
	}

	/**
	 * 校验用户名
	 * 
	 * @param userName
	 * @return
	 */
	public static boolean isUserName(String userName) {
		return null != userName && userName.matches(USERNAME);
	}

	/**
	 * 验证只有汉字和字母
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isSinogramOrLetter(String str) {
		return null != str && str.matches(SINOGRAMORLETTER);
	}

	/**
	 * 校验金额 包含正数和负数
	 * 
	 * @param money
	 * @return
	 */
	public static boolean isMoney(String money) {
		return null != money && money.matches(MONEY);
	}

	/**
	 * 校验是正数金额
	 * 
	 * @param money
	 * @return
	 */
	public static boolean isPlusMoney(String money) {
		return null != money && money.matches(PLUSMONEY);
	}

	/**
	 * 验证字符串是负数
	 * 
	 * @param money
	 * @return
	 */
	public static boolean isMinusMoney(String money) {
		return StringUtils.startsWith(money, "-");
	}

	public static void main(String[] args) {
		System.out.println(isMoney("0.02"));
	}
}
